package com.xunmall.gateway.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xunmall.account.interceptor.JCaptchaValidateFilter;
import com.xunmall.gateway.interceptor.*;
import com.xunmall.gateway.interceptor.security.SecCheckFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * Created by owen5 on 2018/1/8.
 */
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();

        ObjectMapper objectMapper = new ObjectMapper();
//		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);	// 排除值为空属性
//		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);// 排除初始值未被改变的属性
//		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        // 进行日期格式化
//		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		objectMapper.setDateFormat(dateFormat);
        // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(customJackson2HttpMessageConverter());
    }

    @Bean
    public FilterRegistrationBean customerCorsFilter(){
        SimpleCORSFilter filter = new SimpleCORSFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean captchaFilter() {
        JCaptchaValidateFilter filter = new JCaptchaValidateFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.addUrlPatterns("/oauth/token");
        registration.setFilter(filter);
        registration.setOrder(-1000);
        return registration;
    }

    @Bean
    public FilterRegistrationBean SecCheckFilter(){
        SecCheckFilter filter = new SecCheckFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(0);
        return registration;
    }

    @Bean
    public FilterRegistrationBean urlCheckFilter() {
        UrlCheckFilter filter = new UrlCheckFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(5);
        return registration;
    }

    @Bean
    public FilterRegistrationBean trackerFilter() {
        TrackerFilter filter = new TrackerFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(1000);
        return registration;
    }

    @Bean
    public ErrorZuulFilter errorZuulFilter(){
        return new ErrorZuulFilter();
    }

    @Bean
    public TrackerZuulFilter trackerZuulFilter(){
        return new TrackerZuulFilter();
    }

    @Bean
    public DebugRouteZuulFilter debugRouteZuulFilter(){
        return new DebugRouteZuulFilter();
    }
}
